<?php
/**
 * Created by PhpStorm.
 * User: lzy
 * Date: 2018/11/21
 * Time: 17:48
 */

namespace App\Content\Controllers\Home;

use App\Base\Controllers\Controller;
use App\Content\Contracts\ArticleInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;


class ArticleController extends Controller
{
    /**
     * 文章接口
     * @return ArticleInterface
     */
    public function getArticleInterface()
    {

        return app()->make(ArticleInterface::class);
    }

    /**
     * 获取条款
     * @param Request $request
     * @return mixed
     */
    public function clause(Request $request)
    {
        return $this->getArticleInterface()->getInfo(3);
    }

    /**
     * 获取条款
     * @param Request $request
     * @return mixed
     */
    public function help(Request $request)
    {
        return $this->getArticleInterface()->getInfo(4);
    }


    /**
     * 新闻列表
     * @param Request $request
     * @return mixed
     */
    public function getArticleClassList(Request $request){
        $where=function($query) use ($request){
            $class=$request->input("class");
            if(!empty($class)){
                $query->where("channel",$class);
            }
        };
        return $this->getArticleInterface()->getArticleList($where);
    }

    /**
     * 新闻详细
     * @param Request $request
     * @return mixed
     * @throws \App\Base\Exceptions\ApiException
     */
    public function getArticleInfo(Request $request){
        $validator = Validator::make($request->all(), [
            'item_id' => 'required',
        ], ["item_id.required" => '文章id是必须的']);
        if ($validator->fails()) {
            $this->throwValidatorException($validator);
        }
        $where=function($query) use ($request){
            $itemId=$request->input("item_id");
            $query->where("item_id",$itemId);
        };
        return $this->getArticleInterface()->getArticleInfo($where);
    }

    public function getArticleCollectionInfo(Request $request){
        $validator = Validator::make($request->all(), [
            'item_id' => 'required',
        ], ["item_id.required" => '文章id是必须的']);
        if ($validator->fails()) {
            $this->throwValidatorException($validator);
        }
        $where=function($query) use ($request){
            $itemId=$request->input("item_id");
            $query->where("item_id",$itemId);
        };
        return $this->getArticleInterface()->getArticleCollectionInfo($where);
    }

    /**
     * 获取新闻渠道
     * @param Request $request
     */
    public function getArticleChannel(Request $request){
        //获取站点id
        $where=function($query) use ($request){
                $query->where("site_id",$request->input('site_id'));
        };
       return $this->getArticleInterface()->getArticleChannel($where);
    }

    /**
     * 文章评论
     * @param Request $request
     */
    public function setArticleComment(Request $request){
       return $this->getArticleInterface()->setArticleComment($request->except("data","token"));
    }

    /**
     * 文章收藏
     * @param Request $request
     * @return mixed
     */
    public function setArticleCollection(Request $request){
        return $this->getArticleInterface()->setArticleCollection($request->except("data","token"));
    }

}